import axiosIns from '../../utils/axios';
import update, { updateChain } from 'immutability-helper-x';
import { commentDetail, postCommentData } from '../../utils/mock';
import { getOrderList } from './order';
/**
|--------------------------------------------------
|  Constants
|--------------------------------------------------
*/
const OPENCOMMENT = 'OPENCOMMENT';
const OPENUNCOMMENT = 'OPENUNCOMMENT';
const CLOSECOMMENT = 'CLOSECOMMENT';
const CLOSEUNCOMMENT = 'CLOSEUNCOMMENT';
const GETDETAIL = 'GETDETAIL';
const SETCOMMENTORDER = 'SETCOMMENTORDER';
const COM_LOADING = 'COM_LOADING';
const POSTING = 'POSTING';

/**
|--------------------------------------------------
| Actions
|--------------------------------------------------
*/
export function openCommentModal() {
  return {
    type: OPENCOMMENT
  };
}
export function closeCommentModal() {
  return {
    type: CLOSECOMMENT
  };
}
export function openUnCommentModal() {
  return {
    type: OPENUNCOMMENT
  };
}
export function closeUnCommentModal() {
  return {
    type: CLOSEUNCOMMENT
  };
}
export function getDetail(params) {
  return async (dispatch, getState) => {
    const { loading } = getState().comment;
    if (loading) return;
    dispatch(fetching());
    const res = await axiosIns('/getCommentDetail');

    const { data } = res;
    dispatch(getDetailSuccess(data));
  };
}

export function setCommentOrder(data) {
  return {
    type: SETCOMMENTORDER,
    order: data
  };
}

export function postComment(params) {
  return async (dispatch, getState) => {
    const { loading } = getState().comment.unCommentModal;
    if (loading) return;
    dispatch(posting());
    const { commentOrder } = getState().comment;
    const { content, fileList } = params;
    const res = axiosIns('/postComment');
    dispatch(getOrderList({ state: 1 }));
    dispatch(closeUnCommentModal());
  };
}
function posting() {
  return {
    type: POSTING
  };
}
function fetching() {
  return {
    type: COM_LOADING
  };
}
function getDetailSuccess(data) {
  return {
    type: GETDETAIL,
    detail: data
  };
}
/**
|--------------------------------------------------
| Action Handlers
|--------------------------------------------------
*/
const ACTION_HANDLERS = {
  [OPENCOMMENT]: state => {
    return update.$set(state, 'commentModal.visible', true);
  },
  [CLOSECOMMENT]: state => {
    return update.$set(state, 'commentModal.visible', false);
  },
  [OPENUNCOMMENT]: state => {
    return update.$set(state, 'unCommentModal.visible', true);
  },
  [CLOSEUNCOMMENT]: state => {
    return updateChain(state)
      .$set('unCommentModal.visible', false)
      .$set('unCommentModal.loading', false)
      .value();
  },
  [GETDETAIL]: (state, action) => {
    return { ...state, detail: action.detail, loading: false };
  },
  [SETCOMMENTORDER]: (state, action) => {
    return { ...state, commentOrder: action.order };
  },
  [COM_LOADING]: state => {
    return { ...state, loading: true };
  },
  [POSTING]: state => {
    return update.$set(state, 'unCommentModal.loading', true);
  }
};
/**
|--------------------------------------------------
| Reducer
|--------------------------------------------------
*/
const initialState = {
  detail: null,
  commentModal: {
    visible: false
  },
  loading: false,
  unCommentModal: {
    visible: false,
    loading: false
  },
  commentOrder: null
};
export default function(state = initialState, action) {
  const handler = ACTION_HANDLERS[action.type];
  return handler ? handler(state, action) : state;
}
